package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.Constants;
import com.ssyc.common.manager.IndicatorManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.ClassInfo;
import com.ssyc.gsk_teacher.fragment.TeacherAllEventFragment;
import com.ssyc.gsk_teacher.fragment.TeacherEventFragment;
import com.ssyc.gsk_teacher.utils.ClassUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.List;


/**
 * 教师端---活动界面
 * Created by Administrator on 2018/1/18 0018.
 */

public class TeacherEventActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private RelativeLayout rlBack;
    private MagicIndicator mMag;
    private SuperViewPager mVp;
    //编辑
    private RelativeLayout rlEdit;

    //加载框
    private RelativeLayout rlLoading;

    //内容布局
    private LinearLayout llContent;

    //导航栏的标题集合
    private String[] mTitles;

    //用于填充ViewPager的Fragment集合
    private List<Fragment> mFragments;

    //是否获取班级列表数据完成的标识,没完成之前点击选择班级的弹框提示正在加载,不允许点击
    private boolean isLoadClassCompleted;

    //记录弹出展示班级列表PopUpWindow的选中位置的值,默认第一个被选中
    private int popSelectedPos = 0;

    //所有班级的列表
    private List<ClassInfo.ListBean> classInfos;

    //界面刷新的标识
    public static final int UPDATASCORE = 0x450;

    //适配器对象
    private VpAdapter adapter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_event;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        //初始化视图
        initView();

        //请求班级信息
        httpClass();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        mMag = findViewById(R.id.mag);
        mVp = findViewById(R.id.vp);
        rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(this);
        rlEdit = findViewById(R.id.rl_edit);
        rlEdit.setOnClickListener(this);
        rlLoading = findViewById(R.id.rl_loading);
        llContent = findViewById(R.id.ll_conent);
        llContent.setVisibility(View.GONE);
    }

    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }


    /**
     * 初始化顶部的导航栏
     */
    private void initMag() {
        if (classInfos == null || classInfos.size() == 0) {

            UiUtils.Toast("暂无班级信息", false);
            return;
        } else {
            mTitles = new String[classInfos.size() + 1];
            for (int i = 0; i < mTitles.length; i++) {
                if (i == 0) {
                    mTitles[0] = "全部";
                } else {
                    mTitles[i] = classInfos.get(i - 1).getName();
                }
            }

            IndicatorManager.setDynamicIndicator(this, mTitles, mVp, mMag, "#999999", "#1ABC9C");

            //初始化Vp
            initVp();
        }
    }


    /**
     * 请求班级信息
     */
    private void httpClass() {
        ClassUtil.httpClass(rlLoading, this, new ClassUtil.onSuccessListener() {
            @Override
            public void onSuccess(List<ClassInfo.ListBean> datas) {
                //获取班级成功后的操作
                if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (null != llContent && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                //回调班级数据
                classInfos = datas;

                //改变加载完成的标记
                isLoadClassCompleted = true;

                //初始化导航栏
                initMag();
            }

            @Override
            public void hasNoData() {
                rlEdit.setVisibility(View.INVISIBLE);
            }
        });
    }

    /**
     * 初始化ViewPager的方法
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            if (i == 0) {
                mFragments.add(TeacherAllEventFragment.newInstance());
            } else {
                //携带班级号过去
                mFragments.add(TeacherEventFragment.newInstance(classInfos.get(i - 1).getClassnum()));
            }
        }
        adapter = new VpAdapter(getSupportFragmentManager());
        mVp.setAdapter(adapter);
        mVp.setOffscreenPageLimit(mFragments.size());
    }

    /**
     * 页面控件点击事件
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_edit) {
            //发布活动
            if (isLoadClassCompleted) {
                //标题栏顶部的编辑,跳转到发布活动界面
                Intent intent = new Intent(this, TeacherSendEventActivity.class);
                //携带班级信息过去
                intent.putExtra("classinfos", GsonUtil.listToJson(classInfos));
                startActivity(intent);
            } else {
                //没加载完成
                SnackbarUtil.ShortSnackbar(llBaseMain, Constants.LOADING, 0).show();
            }

        } else if (id == R.id.rl_back) {
            //返回
            finish();
        }
    }

    /**
     * ViewPager的适配器
     */
    class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

}
